package nuc.soft.A01.jzs.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nuc.soft.A01.jzs.model.Role;
import nuc.soft.A01.jzs.model.general.GeneralUser;
import nuc.soft.A01.jzs.model.user.Student;
import nuc.soft.A01.jzs.model.user.Admin;
import nuc.soft.A01.jzs.services.RoleService;
import nuc.soft.A01.jzs.services.StudentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/student")
public class StudentController {

	@Autowired
	private StudentService service;
	
	@RequestMapping("/showsearch")
	public String getSearchStudent(HttpServletRequest request,
			HttpServletResponse response,String searchname)
	{
		ArrayList<Student> allStudent = service.getSearchStudent(searchname);
		request.setAttribute("students", allStudent);
		return "student/studentindex";
	}

	
	@RequestMapping("/showall")
	public String getAllStudent(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
				ArrayList<Student> allStudent = service.getAllStudent();
				request.setAttribute("students", allStudent);
				return "student/studentindex";
	}

	@RequestMapping("/showadd")
	public String showadd(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
				return "student/add";
	}

	@RequestMapping("/add")
	public String add(HttpServletRequest request, HttpServletResponse response,
			Student Student) throws Exception {
				service.insert(Student);
				return "forward:/student/showall";
	}

	@RequestMapping("/showedit")
	public String showedit(HttpServletRequest request,
			HttpServletResponse response, String theid) throws Exception {
				Student student = service.getStudentById(theid);
				request.setAttribute("editstudent", student);
				return "/student/edit";
	}

	@RequestMapping("/edit")
	public String edit(HttpServletRequest request,
			HttpServletResponse response, Student student, int tochoose)
			throws Exception {
					
				student.setPass(student.getStudentid());
				service.update(student);
				return "forward:/student/showall";
		
	}
}
